﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class NewsType : PowerBase
    {
        CJJCH.BLL.T_NET_MenuSecond bll_menuSecond = new CJJCH.BLL.T_NET_MenuSecond();
        CJJCH.BLL.T_NET_MenuThird bll_menuthird = new CJJCH.BLL.T_NET_MenuThird();
        CJJCH.Model.T_NET_MenuThird model = new CJJCH.Model.T_NET_MenuThird();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.form1.DefaultButton = "btnSearch";

            if (!IsPostBack)
            {
                InitControl();
                InitRequest();
                Repeater_DataBind();
            }
        }
        private void InitControl()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"].Trim()))
            {
                CJJCH.Model.T_NET_MenuSecond model = new CJJCH.Model.T_NET_MenuSecond();
                model = bll_menuSecond.GetMenuSecondModel(Convert.ToInt32(Request.QueryString["secondid"].ToString()));
                if (model != null)
                {
                    this.lblSecondID.Text = model.ID.ToString();
                    this.NewsTop1.SecondId = model.ID;
                    this.lblSecondName.Text = model.MenuName;
                    this.NewsTop1.SecondName = model.MenuName;
                    if (!model.HaveChild)
                    {

                        this.NewsTop1.TypeID = 0;
                    }
                    else
                    {
                        this.NewsTop1.TypeID = 1;
                    }
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }
        }
        protected void InitRequest()
        {
            if (Request.QueryString["keyword"] != null)
            {
                lblkeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].ToString());
                txtKeyword.Text = lblkeyword.Text.Trim();
            }
        }

        protected void Repeater_DataBind()
        {
            IList<CJJCH.Model.T_NET_MenuThird> list = new List<CJJCH.Model.T_NET_MenuThird>();
            list = bll_menuthird.GetThird(Convert.ToInt32(this.lblSecondID.Text.Trim()), this.lblkeyword.Text.Trim());
            this.rptMenuThird.DataSource = list;
            this.rptMenuThird.DataBind();
            if (list.Count == 0)
            {
                litMessage.Text = "<table width=100%><tr style=height:18px bgcolor=#FFFFFF><td align='center'><font color=red>对不起，系统没有检索出有关信息！<font></td></tr></table>";
            }
            else
            {
                litMessage.Text = "";
            }
        }

        protected void rptMenuThird_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((TextBox)e.Item.FindControl("txtMenuName")).Text = ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).MenuName;
                ((TextBox)e.Item.FindControl("txtOrderNum")).Text = ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).OrderNum.ToString();
                ((LinkButton)e.Item.FindControl("lbtnEdit")).CommandArgument = ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).Attributes.Add("onclick", "javascript:if(confirm('确定删除该类别吗？')){return true;}else{return false;}");
                ((LinkButton)e.Item.FindControl("lbtnDel")).CommandArgument = ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).ID.ToString();
            }
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnAdd_Click(object sender, EventArgs e)
        {
            string error = "";
            error = CheckInput();
            if (!string.IsNullOrEmpty(error))
            {
                LTP.Common.MessageBox.Show(this, error);
                return;
            }
            model.MenuName = LTP.Common.StringClass.EncodeString(this.txtMenuName.Text.Trim());
            model.OrderNum = Convert.ToInt32(this.txtOrderNum.Text.Trim());
            model.SecondID = Convert.ToInt32(this.lblSecondID.Text.Trim());
            string res = bll_menuthird.AddThird(model);
            if (res == "")
            {
                LTP.Common.WindowClass.WindowGoHref("NewsType.aspx?secondid="
                    + LTP.Common.StringClass.EncodeUrl(this.lblSecondID.Text.Trim()));
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(res);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.lblkeyword.Text = LTP.Common.StringClass.EncodeString(this.txtKeyword.Text.Trim());
            Repeater_DataBind();

        }
        /// <summary>
        /// 修改
        /// </summary>
        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            StringBuilder error = new StringBuilder();
            LinkButton linb = (LinkButton)sender;
            int Index = ((RepeaterItem)(linb.NamingContainer)).ItemIndex;
            TextBox txtMenuName = (TextBox)rptMenuThird.Items[Index].FindControl("txtMenuName");
            TextBox txtOrderNum = (TextBox)rptMenuThird.Items[Index].FindControl("txtOrderNum");
            if (txtMenuName.Text.Trim() == "")
            {
                error.Append("请输入类别名称！！\\n");
            }
            if (txtOrderNum.Text.Trim() == "")
            {
                error.Append("请输入排序号！！\\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(txtOrderNum.Text.Trim()))
            {
                error.Append("排序号必须是数字！！\\n");
            }

            if (!string.IsNullOrEmpty(error.ToString()))
            {
                LTP.Common.MessageBox.Show(this, error.ToString());
                return;
            }
            model.ID = int.Parse(linb.CommandArgument);
            model.MenuName = LTP.Common.StringClass.EncodeString(txtMenuName.Text.Trim());
            model.OrderNum = Convert.ToInt32(txtOrderNum.Text.Trim());
            model.SecondID = Convert.ToInt32(lblSecondID.Text.Trim());

            string res = bll_menuthird.UpdateThird(model);
            LTP.Common.MessageBox.ShowAndRedirect(this, res, "NewsType.aspx?keyword=" + LTP.Common.StringClass.EncodeUrl(lblkeyword.Text.Trim())
                + "&secondid=" + LTP.Common.StringClass.EncodeUrl(this.lblSecondID.Text.Trim()));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        protected void lbtbDel_Click(object sender, EventArgs e)
        {
            LinkButton linb = (LinkButton)sender;
            string res = bll_menuthird.DelThird(Convert.ToInt32(linb.CommandArgument));
            if (res == "")
            {
                Response.Redirect("NewsType.aspx?keyword=" + LTP.Common.StringClass.EncodeUrl(lblkeyword.Text.Trim())
                    + "&secondid=" + LTP.Common.StringClass.EncodeUrl(this.lblSecondID.Text.Trim()));
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(res);
            }
        }

        protected string CheckInput()
        {
            StringBuilder error = new StringBuilder();
            if (txtMenuName.Text.Trim() == "")
            {
                error.Append("请输入类别名称！\\n");
            }
            if (txtOrderNum.Text.Trim() == "")
            {
                error.Append("请输入排序号！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(txtOrderNum.Text.Trim()))
            {
                error.Append("排序号必须是数字！ \\n");
            }
            return error.ToString();
        }

    }
}